package com.fenghao.subject.common.enums;

import lombok.Getter;

@Getter
public enum ResultCodeEnum {

    SUCCESS(200, "成功"),
    FAIL(500, "失败");

    private final int code;
    private final String desc;

    ResultCodeEnum(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    // 通过 code 获取枚举
    public static ResultCodeEnum getEnumByCode(int codeVal) {
        ResultCodeEnum[] resultCodeEnums = ResultCodeEnum.values();
        for (ResultCodeEnum resultCodeEnum : resultCodeEnums) {
            if (resultCodeEnum.code == codeVal) {
                return resultCodeEnum;
            }
        }
        return null;
    }

    // 通过 desc 获取枚举
    public static ResultCodeEnum getEnumByDesc(String descVal) {
        ResultCodeEnum[] resultCodeEnums = ResultCodeEnum.values();
        for (ResultCodeEnum resultCodeEnum : resultCodeEnums) {
            if (resultCodeEnum.desc.equals(descVal)) {
                return resultCodeEnum;
            }
        }
        return null;
    }
}
